<template>
  <div>
    <el-form ref="userForm" style="width:860px;padding: 20px;" label-width="220px" :model="userInfo" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="userInfo.username" size="mini" class="common" />
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="userInfo.workNumber" disabled size="mini" class="common" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="userInfo.mobile" :disabled="!!$route.params.id" size="mini" class="common" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentId">
        <!-- 封装组件 -->
        <Cascader v-model="userInfo.departmentId" class="common" />

      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="userInfo.formOfEmployment" size="mini" class="common">
          <el-option size="mini" label="正式" :value="1" />
          <el-option size="mini" label="非正式" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          placeholder="选择日期"
          size="mini"
          class="common"
          style="width: 300px;"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="userInfo.correctionTime"
          type="date"
          placeholder="选择日期"
          size="mini"
          class="common"
          style="width: 300px;"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="员工头像">
        <imageUpload v-model="userInfo.staffPhoto" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="margin-left: 10px;" size="mini" @click="saveUpdate">保存更新</el-button>
      </el-form-item>

    </el-form>

  </div>
</template>

<script>
import { addUser, updateUser, getInfo } from '@/api/employee'
import Cascader from './cascader.vue'
import imageUpload from './image-upload.vue'
export default {
  components: {
    Cascader,
    imageUpload
  },
  data() {
    return {
      userInfo: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: null, // 聘用形式
        departmentId: null, // 部门id
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户名1-4个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '转正时间不能为空', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (this.userInfo.timeOfEntry > +new Date(value)) {
              callback('转正时间不能低于入职时间')
            } else {
              callback()
            }
          } }
        ],
        departmentId: [
          { required: true, message: '部门不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.$route.params.id && this.getInfo()
  },
  methods: {
    saveUpdate() {
      this.$refs.userForm.validate(async isOk => {
        if (isOk) {
          if (this.$route.params.id) {
            await updateUser(this.userInfo)
            this.$message.success('修改成功')
          } else {
            await addUser(this.userInfo)
            this.$message.success('添加成功')
          }
          this.$router.push('/employee')
        }
      })
    },
    async getInfo() {
      this.userInfo = await getInfo(this.$route.params.id)
    }
  }
}
</script>

<style>
.common{
    margin: 0;
    width: 300px;
    height: 40px;
}
</style>
